package com.rosan.installer.ui.page.main.settings.preferred.subpage.home

import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import com.mikepenz.aboutlibraries.ui.compose.android.produceLibraries
import com.mikepenz.aboutlibraries.ui.compose.m3.LibrariesContainer
import com.rosan.installer.R
import com.rosan.installer.ui.page.main.widget.card.StatusWidget
import com.rosan.installer.ui.page.main.widget.setting.AppBackButton
import com.rosan.installer.ui.page.main.widget.setting.LabelWidget

@OptIn(ExperimentalMaterial3Api::class, ExperimentalMaterial3ExpressiveApi::class)
@Composable
fun HomePage(
    navController: NavController
) {
    // from https://github.com/mikepenz/AboutLibraries#setup
    // Android: Provide resource identifier for the `R.raw.aboutlibraries` file.
    // This file is generated by the AboutLibraries Gradle plugin.
    val libraries by produceLibraries(R.raw.aboutlibraries)

    val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()

    Scaffold(
        modifier = Modifier
            .nestedScroll(scrollBehavior.nestedScrollConnection)
            .fillMaxSize(),
        topBar = {
            TopAppBar(
                title = {
                    Text(text = stringResource(id = R.string.about))
                },
                scrollBehavior = scrollBehavior,
                navigationIcon = { AppBackButton(onClick = { navController.navigateUp() }) }
            )
        },
    ) { paddingValues ->
        /**
         * @author iamr0s
         */
        /*LazyColumn(
            contentPadding = PaddingValues(16.dp),
            modifier = Modifier
                .fillMaxSize()
                .padding(it),
            verticalArrangement = Arrangement.spacedBy(16.dp),
        ) {
            pkg {
                StatusWidget()
            }
            *//*            pkg {
                            DonateWidget()
                        }
                        pkg {
                            DiscussWidget()
                        }*//*
        }*/
        /**
         * @author wxxsfxyzm
         */
        LibrariesContainer(
            libraries = libraries,
            modifier = Modifier
                .fillMaxSize()
                .padding(horizontal = 16.dp),
            contentPadding = paddingValues,// PaddingValues(horizontal = 16.dp),
            divider = {
                // Spacer(modifier = Modifier.height(16.dp))
                // 线 + 间距
                HorizontalDivider(modifier = Modifier.padding(vertical = 8.dp))
            },
            header = {
                item { StatusWidget() }
                item { LabelWidget(stringResource(R.string.open_source_license), 18.dp) }
            }
        )
    }
}

/*
/**
 * @author iamr0s
 */
@Composable
fun DonateWidget() {
    val context = LocalContext.current

    val items = listOf(
        HomeCardItem(
            label = stringResource(id = R.string.alipay),
            onClick = {
                openUrl(context, "https://qr.alipay.com/fkx18580lfpydiop04dze47")
            }
        ),
        HomeCardItem(
            label = stringResource(id = R.string.wechat),
            onClick = {
                openUrl(context, "https://missuo.ru/file/fee5df1381671c996b127.png")
            }
        ),
        HomeCardItem(
            label = stringResource(id = R.string.binance),
            onClick = {
                openUrl(context, "https://missuo.ru/file/28368c28d4ff28d59ed4b.jpg")
            }
        ),
    )
    ItemsCardWidget(
        title = {
            Text(text = stringResource(id = R.string.donate))
        },
        items = items
    )
}

@Composable
fun DiscussWidget() {
    val context = LocalContext.current

    val items = listOf(
        HomeCardItem(
            label = stringResource(id = R.string.qq_channel),
            onClick = {
                openUrl(
                    context,
                    "https://pd.qq.com/s/nx7jpup8"
                )
            }
        ),
        HomeCardItem(
            label = stringResource(id = R.string.qq_group_official),
            onClick = {
                openUrl(
                    context,
                    "https://qm.qq.com/cgi-bin/qm/qr?k=YMyAigxnns_FkISlRaormMiApHr2RmU7&jump_from=webapi&qr=1"
                )
            }
        ),
        HomeCardItem(
            label = stringResource(id = R.string.telegram_group),
            onClick = {
                openUrl(context, "https://t.me/rosan_installer")
            }
        ),
    )
    ItemsCardWidget(
        title = {
            Text(text = stringResource(id = R.string.discuss))
        },
        items = items
    )
}*/